package com.fzhucc.modules.security.rest;


import com.fzhucc.annotation.Log;
import com.fzhucc.modules.security.domain.UserCode;
import com.fzhucc.modules.security.domain.dto.UserCodeQueryCriteria;
import com.fzhucc.modules.security.service.UserCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author yyg
* @date 2023-10-07
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "userCode管理")
@RequestMapping("/api/userCode")
public class UserCodeController {

    private final UserCodeService userCodeService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('userCode:list')")
    public void exportUserCode(HttpServletResponse response, UserCodeQueryCriteria criteria) throws IOException {
        userCodeService.download(userCodeService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询userCode")
    @ApiOperation("查询userCode")
    @PreAuthorize("@el.check('userCode:list')")
    public ResponseEntity<Object> queryUserCode(UserCodeQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(userCodeService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增userCode")
    @ApiOperation("新增userCode")
    @PreAuthorize("@el.check('userCode:add')")
    public ResponseEntity<Object> createUserCode(@Validated @RequestBody UserCode resources){
        return new ResponseEntity<>(userCodeService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改userCode")
    @ApiOperation("修改userCode")
    @PreAuthorize("@el.check('userCode:edit')")
    public ResponseEntity<Object> updateUserCode(@Validated @RequestBody UserCode resources){
        userCodeService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除userCode")
    @ApiOperation("删除userCode")
    @PreAuthorize("@el.check('userCode:del')")
    public ResponseEntity<Object> deleteUserCode(@RequestBody Integer[] ids) {
        userCodeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}